package com.yunchen.server.service;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunchen.server.mapper.BaseDaoMapper;
import com.yunchen.server.mapper.RolePermissionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;

@Service
public class RolePermissionsService {
    @Autowired
    private BaseDaoMapper baseDaoMapper;
    @Autowired
    private RolePermissionsMapper rolePermissionsMapper;

    public JSONArray List (JSONObject json) {
        String SQL="SELECT a.permission_id,a.permission_name" +
                " from permissions a where a.permission_type=0 and a.parent_id=0  ORDER BY sort_order  asc   ";
        List<LinkedHashMap<String, Object>> linkedHashMaps = baseDaoMapper.selectSql(SQL);
        JSONArray listArray = JSON.parseArray(JSON.toJSONString(linkedHashMaps));
        JSONArray array=new JSONArray();
        for (int i=0;i<listArray.size();i++){
            JSONObject jsonObject=new JSONObject();
            Integer  permissionId = listArray.getJSONObject(i).getInteger("permission_id");
            jsonObject.put("permission_id",permissionId);
            jsonObject.put("permission_name",listArray.getJSONObject(i).getString("permission_name"));

            String SQL1="SELECT a.permission_id,a.permission_name " +
                    " from permissions a where  a.parent_id="+permissionId+"  ORDER BY permission_type asc,sort_order  asc   ";
            List<LinkedHashMap<String, Object>> linkedHashMaps1 = baseDaoMapper.selectSql(SQL1);
            JSONArray listArray1 = JSON.parseArray(JSON.toJSONString(linkedHashMaps1));
            JSONArray array1=new JSONArray();
            for (int k=0;k<listArray1.size();k++){
                JSONObject jsonObject1=new JSONObject();
                Integer  permissionId1 = listArray1.getJSONObject(k).getInteger("permission_id");
                jsonObject1.put("permission_id",permissionId1);
                jsonObject1.put("permission_name",listArray1.getJSONObject(k).getString("permission_name"));
                String SQL2="SELECT a.permission_id,a.permission_name  " +
                        " from permissions a where a.permission_type=1 and a.parent_id="+permissionId1+"  ORDER BY sort_order  asc   ";
                List<LinkedHashMap<String, Object>> linkedHashMaps2 = baseDaoMapper.selectSql(SQL2);
                JSONArray listArray2 = JSON.parseArray(JSON.toJSONString(linkedHashMaps2));
                if (listArray2.size()>0){
                    jsonObject1.put("children",listArray2);
                }
                array1.add(jsonObject1);
            }
            if (array1.size()>0){
                jsonObject.put("children",array1);
            }
            array.add(jsonObject);
        }
        return array;
    }




    public JSONArray getList (JSONObject json) {
        System.out.println("访问成功，当前用户ID: " + StpUtil.getLoginIdAsString());

        String SQL="SELECT a.permission_id,a.permission_code ,a.permission_name,a.resource_path,a.icon" +
                " from permissions a where a.permission_type=0 and a.parent_id=0  ORDER BY sort_order  asc   ";
        List<LinkedHashMap<String, Object>> linkedHashMaps = baseDaoMapper.selectSql(SQL);
        JSONArray listArray = JSON.parseArray(JSON.toJSONString(linkedHashMaps));
        JSONArray array=new JSONArray();
        for (int i=0;i<listArray.size();i++){
            JSONObject jsonObject=new JSONObject();
            Integer  permissionId = listArray.getJSONObject(i).getInteger("permission_id");
            jsonObject.put("component",listArray.getJSONObject(i).getString("permission_code"));
            jsonObject.put("name",listArray.getJSONObject(i).getString("permission_name"));
            jsonObject.put("path",listArray.getJSONObject(i).getString("resource_path"));
            jsonObject.put("icon",listArray.getJSONObject(i).getString("icon"));

            String SQL1="SELECT a.permission_id,a.permission_code ,a.permission_name,a.resource_path,a.icon " +
                    " from permissions a where a.permission_type=0 and  a.parent_id="+permissionId+"  ORDER BY permission_type asc,sort_order  asc   ";
            List<LinkedHashMap<String, Object>> linkedHashMaps1 = baseDaoMapper.selectSql(SQL1);
            JSONArray listArray1 = JSON.parseArray(JSON.toJSONString(linkedHashMaps1));
            JSONArray array1=new JSONArray();
            for (int k=0;k<listArray1.size();k++){
                JSONObject jsonObject1=new JSONObject();
                Integer  permissionId1 = listArray1.getJSONObject(k).getInteger("permission_id");
                jsonObject1.put("component",listArray1.getJSONObject(k).getString("permission_code"));
                jsonObject1.put("name",listArray1.getJSONObject(k).getString("permission_name"));
                jsonObject1.put("path",listArray1.getJSONObject(k).getString("resource_path"));
                jsonObject1.put("icon",listArray1.getJSONObject(k).getString("icon"));
                String SQL2="SELECT a.permission_id,a.permission_name  " +
                        " from permissions a where a.permission_type=1 and a.parent_id="+permissionId1+"    ";
                List<LinkedHashMap<String, Object>> linkedHashMaps2 = baseDaoMapper.selectSql(SQL2);
                JSONArray listArray2 = JSON.parseArray(JSON.toJSONString(linkedHashMaps2));
                jsonObject1.put("addChildren",listArray2);
                array1.add(jsonObject1);
            }
            if (array1.size()>0){
                jsonObject.put("children",array1);
            }
            String SQL3="SELECT a.permission_id,a.permission_code ,a.permission_name,a.resource_path,a.icon " +
                    " from permissions a where a.permission_type=1 and  a.parent_id="+permissionId+"  ORDER BY permission_type asc,sort_order  asc   ";
            List<LinkedHashMap<String, Object>> linkedHashMaps3 = baseDaoMapper.selectSql(SQL3);
            JSONArray listArray3 = JSON.parseArray(JSON.toJSONString(linkedHashMaps3));
            jsonObject.put("addChildren",listArray3);
            array.add(jsonObject);
        }
        return array;
    }
}
